/*
 * @Author: 李琪 liqi@lark2game.com
 * @Date: 2025-08-21 10:41:17
 * @LastEditors: 李琪 liqi@lark2game.com
 * @LastEditTime: 2025-08-21 10:48:59
 * @FilePath: /gameWorld/assets/script/game/account/view/EnemyGenerator.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Node, Prefab, instantiate, CCInteger, CCFloat } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnemyGenerator')
export class EnemyGenerator extends Component {
    @property(Prefab)
    enemyPrefab: Prefab = null!; // 敌人预制体

    @property(CCInteger)
    maxEnemies: number = 5; // 最大敌人数

    @property(CCFloat)
    spawnInterval: number = 3.0; // 生成间隔

    private _spawnTimer: number = 0; // 生成计时器

    start() {
        // 初始生成一些敌人
        for (let i = 0; i < this.maxEnemies; i++) {
            this.spawnEnemy();
        }
    }

    update(deltaTime: number) {
        // 生成敌人逻辑
        // this._spawnTimer += deltaTime;
        // if (this._spawnTimer >= this.spawnInterval && this.node.children.length < this.maxEnemies) {
        //     this._spawnTimer = 0;
        //     this.spawnEnemy();
        // }
    }

    // 生成敌人
    spawnEnemy() {
        if (!this.enemyPrefab) return;

        // 实例化敌人
        const enemy = instantiate(this.enemyPrefab);
        if (!enemy) return;

        // 随机位置
        const x = Math.random() * 400 - 200;
        const y = Math.random() * 300 + 500;
        enemy.setPosition(x, y, 0);

        // 添加到场景中
        this.node.addChild(enemy);
    }

    // 清空所有敌人
    clearAllEnemies() {
        for (const child of this.node.children) {
            child.destroy();
        }
    }
}